#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
// #include <stdbool.h>
int is_positive_integer(const char *str) {
    while (*str) {
        if (!isdigit(*str)) {
            return 0;
        }
        str++;
    }
    return 1;
}

static volatile int run = 1;

void sigterm(int sig) {
    run = 0;
}

int main(int argc, char *argv[]) {
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <file_path> <start_line>\n", argv[0]);
        return 1;
    }
    signal(SIGINT, sigterm);
    signal(SIGTERM, sigterm);
    signal(SIGKILL, sigterm);
    char *path = argv[1];
    char *start_line_str = argv[2];

    if (!is_positive_integer(start_line_str)) {
        fprintf(stderr, "Parameter 2 must be a positive integer.\n");
        return 1;
    }

    int start_line = atoi(start_line_str);
    FILE *file = fopen(path, "r");

    if (!file) {
        fprintf(stderr, "File '%s' not found\n", path);
        return 1;
    }

    char rate[10] = "";
    char pitch[10] = "";

    // Check for tts_setting.txt file and read settings
    FILE *settings_file = fopen("./tts_setting.txt", "r");
    int settings_file_exists = settings_file != NULL;
    if (settings_file) {
        char key[10];
        char value[10];
        while (fscanf(settings_file, "%9[^:]:%9s\n", key, value) == 2) {
            if (strcmp(key, "rate") == 0) {
                strcpy(rate, value);
            } else if (strcmp(key, "p") == 0) {
                strcpy(pitch, value);
            } else {
                printf("Unknown setting in tts_setting.txt: %s\n", key);
            }
        }
        fclose(settings_file);
        printf("./tts_setting.txt found\n");
        printf("rate:%s\n", rate);
        printf("p:%s\n", pitch);
    }

    char *line = NULL;
    size_t len = 0;
    size_t read;
    int current_line = 0;
    char command[4096];

    if (settings_file_exists) {
        while (run && (read = getline(&line, &len, file)) != -1) {
            current_line++;

            if (current_line >= start_line) {
                // Trim line and check if it's empty
                char *trimmed_line = line;
                while (isspace((unsigned char)*trimmed_line)) trimmed_line++;
                if (*trimmed_line == '\0') continue;

                printf("Reading line %d\n", current_line);

                // Create the command string
                
                snprintf(command, sizeof(command), "termux-tts-speak %s%s %s%s \"%s\"\0",
                        rate[0] ? "-r " : "", rate,
                        pitch[0] ? "-p " : "", pitch,
                        trimmed_line);
                
                // Execute the termux-tts-speak command
                system(command);
            }
            if (!run) {
                printf("Termination signal received, stopping...\n");
                break;
            }
        }

        free(line);
    }else{
        while (run && (read = getline(&line, &len, file)) != -1) {
            current_line++;

            if (current_line >= start_line) {
                // Trim line and check if it's empty
                char *trimmed_line = line;
                while (isspace((unsigned char)*trimmed_line)) trimmed_line++;
                if (*trimmed_line == '\0') continue;

                printf("Reading line %d\n", current_line);

                // Create the command string
                snprintf(command, sizeof(command), "termux-tts-speak \"%s\"\0",
                        trimmed_line);
                
                // Execute the termux-tts-speak command
                system(command);
            }
            if (!run) {
                printf("Termination signal received, stopping...\n");
                break;
            }
        }

        free(line);
    }

    fclose(file);

    return 0;
}